<?php 
class ControllerPhoneConfig extends Controller {
	private $error = array(); 

	public function index() {
		$this->language->load('phone/config');

		$this->document->setTitle($this->language->get('heading_title')); 
		
		$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->data['text_link'] = $this->language->get('text_link');
		$this->data['text_limit'] = $this->language->get('text_limit');
		
		$this->data['link_facebook'] = $this->language->get('link_facebook');
		$this->data['link_twitter'] = $this->language->get('link_twitter');
		$this->data['link_google_plus'] = $this->language->get('link_google_plus');
		$this->data['top_search'] = $this->language->get('top_search');
		$this->data['top_spam'] = $this->language->get('top_spam');
		$this->data['comments'] = $this->language->get('comments');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		
		$this->load->model('setting/setting');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_setting_setting->editSetting('phone_config', $this->request->post);
		
			$this->session->data['success'] = $this->language->get('text_success');
		}

		$this->data['breadcrumbs'][] = array(
				'text'      => $this->language->get('text_home'),
				'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
				'separator' => false
		);
		
		$this->data['breadcrumbs'][] = array(
				'text'      => $this->language->get('heading_title'),
				'href'      => $this->url->link('phone/config', 'token=' . $this->session->data['token'], 'SSL'),
				'separator' => ' :: '
		);
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
		if (isset($this->session->data['error_warning'])) {
			$this->data['error_warning'] = $this->session->data['error_warning'];
		
			unset($this->session->data['error_warning']);
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->request->post['phone_config_facebook'])) {
			$this->data['facebook'] = $this->request->post['phone_config_facebook'];
		} else {
			$this->data['facebook'] = $this->config->get('phone_config_facebook');
		}
		
		if (isset($this->request->post['phone_config_twitter'])) {
			$this->data['twitter'] = $this->request->post['phone_config_twitter'];
		} else {
			$this->data['twitter'] = $this->config->get('phone_config_twitter');
		}
		
		if (isset($this->request->post['phone_config_google_plus'])) {
			$this->data['google_plus'] = $this->request->post['phone_config_google_plus'];
		} else {
			$this->data['google_plus'] = $this->config->get('phone_config_google_plus');
		}
		
		if (isset($this->request->post['phone_config_top_search_limit'])) {
			$this->data['top_search_limit'] = $this->request->post['phone_config_top_search_limit'];
		} else {
			$this->data['top_search_limit'] = $this->config->get('phone_config_top_search_limit');
		}
		
		if (isset($this->request->post['phone_config_top_spam_limit'])) {
			$this->data['top_spam_limit'] = $this->request->post['phone_config_top_spam_limit'];
		} else {
			$this->data['top_spam_limit'] = $this->config->get('phone_config_top_spam_limit');
		}
		
		if (isset($this->request->post['phone_config_comment_limit'])) {
			$this->data['comment_limit'] = $this->request->post['phone_config_comment_limit'];
		} else {
			$this->data['comment_limit'] = $this->config->get('phone_config_comment_limit');
		}
		
		$this->data['action'] = $this->url->link('phone/config', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['cancel'] = $this->url->link('phone/config', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['token'] = $this->session->data['token'];
		
		if (isset($this->request->post['config_link_facebook'])) {
			$this->data['config_link_facebook'] = $this->request->post['config_link_facebook'];
		} else {
			$this->data['config_link_facebook'] = $this->config->get('config_link_facebook');
		}
		
		if (isset($this->request->post['config_link_twitter'])) {
			$this->data['config_link_twitter'] = $this->request->post['config_link_twitter'];
		} else {
			$this->data['config_link_twitter'] = $this->config->get('config_link_twitter');
		}
		
		if (isset($this->request->post['config_link_google_plus'])) {
			$this->data['config_link_google_plus'] = $this->request->post['config_link_google_plus'];
		} else {
			$this->data['config_link_google_plus'] = $this->config->get('config_link_google_plus');
		}
		
		$this->template = 'phone/config.tpl';
		$this->children = array(
				'common/header',
				'common/footer'
		);
		
		$this->response->setOutput($this->render());
	}
	
	protected function validate() {
		if (!$this->user->hasPermission('modify', 'phone/config')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}
}
?>